package ch.fusun.baron.city.ui;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import ch.fusun.baron.coatofarms.ui.CoatOfArmsViewer;
import ch.fusun.baron.player.Dynasty;

/**
 * The activator
 */
public class Activator extends AbstractUIPlugin {

    /**
     * The plug-in ID
     */
    public static final String PLUGIN_ID = "ch.fusun.baron.city.ui"; //$NON-NLS-1$
    private static final String ICONS_EMPTY = "icons/empty.png"; //$NON-NLS-1$
    private static final String ICONS_PIRATE = "icons/pirate.png"; //$NON-NLS-1$

    // The shared instance
    private static Activator plugin;

    /**
     * The constructor
     */
    public Activator() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
     * )
     */
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
     * )
     */
    @Override
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

    /**
     * Returns an image descriptor for the image file at the given plug-in
     * relative path
     * 
     * @param path
     *            the path
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path) {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    /**
     * @param path
     *            the path to the image
     * @return The image
     */
    public static Image getImage(String path) {
        Image image = getDefault().getImageRegistry().get(path);
        if (image == null) {
            image = getImageDescriptor(path).createImage();
            getDefault().getImageRegistry().put(path, image);
        }
        return image;
    }

    /**
     * @param dynasty
     *            The dynasty to draw
     * @return The rendered image
     */
    public static Image getImageForDynasty(Dynasty dynasty) {
        if (dynasty == null) {
            return Activator.getImage(ICONS_PIRATE);
        }
        Image image = getDefault().getImageRegistry().get(dynasty.getName());
        if (image == null) {
            Image empty = Activator.getImage(ICONS_EMPTY);
            image = new Image(empty.getDevice(), empty, SWT.IMAGE_COPY);
            CoatOfArmsViewer.drawOnGC(dynasty.getCoatOfArms(), image, 32, 32,
                    10, 10);
            getDefault().getImageRegistry().put(dynasty.getName(), image);
        }

        return image;
    }
}
